package company.huawei.pro;

import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
    }

    static void p3() {
        Map<String, Integer> map = new HashMap<>();
        map.put("J", 11);
        map.put("Q", 12);
        map.put("K", 13);
        Map<Integer, List<String>> cardMap = new HashMap<>();
        Scanner in = new Scanner(System.in);
        boolean hasA = Boolean.FALSE;
        for (int i = 0; i < 5; ++i) {
            String[] split = in.nextLine().split(" ");
            String s1 = split[0];
            String s2 = split[1];
            if (s1.equals("A")) {
                hasA = Boolean.TRUE;
                if (cardMap.containsKey(1) && cardMap.containsKey(14)) {
                    cardMap.get(1).add(s2);
                    cardMap.get(14).add(s2);
                    continue;
                }
                if (cardMap.containsKey(1) && !cardMap.containsKey(14)) {
                    cardMap.get(1).add(s2);
                    List<String> list = new ArrayList<>();
                    list.add(s2);
                    cardMap.put(14, list);
                    continue;
                }
                if (!cardMap.containsKey(1) && cardMap.containsKey(14)) {
                    cardMap.get(14).add(s2);
                    List<String> list = new ArrayList<>();
                    list.add(s2);
                    cardMap.put(1, list);
                    continue;
                }
                List<String> list1 = new ArrayList<>();
                list1.add(s2);
                List<String> list2 = new ArrayList<>();
                list2.add(s2);
                cardMap.put(1, list1);
                cardMap.put(14, list2);
                continue;
            }
            if (map.containsKey(s1)) {
                Integer key = map.get(s1);
                if (cardMap.containsKey(key)) {
                    cardMap.get(key).add(s2);
                } else {
                    List<String> list = new ArrayList<>();
                    list.add(s2);
                    cardMap.put(key, list);
                }
                continue;
            }
            int key = Integer.parseInt(s1);
            if (cardMap.containsKey(key)) {
                cardMap.get(key).add(s2);
            } else {
                List<String> list = new ArrayList<>();
                list.add(s2);
                cardMap.put(key, list);
            }
        }
        in.close();
        // 判断牌型
        // 同花顺 5 1 同花 5 4 顺子 5 5 其它 5 7
        // 四条 2 2 葫芦 2 3
        // 三条 3 6
        int res = 7;
        int len = cardMap.size();
        if (hasA) len -= 1;
        if (len == 3) {
            res = Math.min(res, 6);
        }
        if (len == 2) {
            for (Map.Entry<Integer, List<String>> item : cardMap.entrySet()) {
                int size = item.getValue().size();
                if (size == 1 || size == 4) {
                    res = Math.min(res, 2);
                } else {
                    res = Math.min(res, 3);
                }
            }
        }
        if (len == 5) {
            Boolean shunzi;
            // 判断是不是顺子
            List<Integer> collect = cardMap.keySet().stream().sorted().collect(Collectors.toList());
            if (hasA) {
                List<Integer> integers1 = collect.subList(0, 5);
                List<Integer> integers2 = collect.subList(1, 6);
                int i1 = integers1.get(4) - integers1.get(0);
                int i2 = integers2.get(4) - integers2.get(0);
                shunzi = i1 == 4 || i2 == 4;
            } else {
                int i = collect.get(4) - collect.get(0);
                shunzi = i == 4;
            }
            if (shunzi) res = Math.min(res, 5);
            // 判断是不是同花
            boolean tonghua = Boolean.FALSE;
            Set<String> set = new HashSet<>();
            cardMap.values().forEach(set::addAll);
            if (set.size() == 1){
                tonghua = true;
            }
            if (tonghua) {
                res = Math.min(res, 4);
            }
            if (tonghua && shunzi) {
                res = Math.min(res, 1);
            }
        }
        System.out.println(res);
    }

    static void p22() {
        Scanner in = new Scanner(System.in);
        String s1 = in.nextLine();
        String s2 = in.nextLine();
        in.close();
        String[] split1 = s1.split(" ");
        String[] split2 = s2.split(" ");
        int self = Integer.parseInt(split1[0]);
        String res = Arrays.stream(split2).map(Integer::parseInt).sorted((v1, v2) -> {
            int abs1 = Math.abs(v1 - self);
            int abs2 = Math.abs(v2 - self);
            if (abs1 != abs2) return abs1 - abs2;
            return v1 - v2;
        }).map(String::valueOf).collect(Collectors.joining(" "));
        System.out.println(res);
    }

    static void p2() {
        Scanner in = new Scanner(System.in);
        String s1 = in.nextLine();
        String s2 = in.nextLine();
        in.close();
        String[] split1 = s1.split(" ");
        String[] split2 = s2.split(" ");
        int self = Integer.parseInt(split1[0]);
        Map<Integer, String> map = Arrays.stream(split2)
                .map(Integer::parseInt)
                .collect(Collectors.toMap(item -> Math.abs(item - self), String::valueOf, (v1, v2) -> {
                    if (Integer.parseInt(v1) > Integer.parseInt(v2)) {
                        return v2 + " " + v1;
                    } else {
                        return v1 + " " + v2;
                    }
                }));
        List<Integer> collect1 = map.keySet().stream().sorted().collect(Collectors.toList());
        StringJoiner builder = new StringJoiner(" ");
        for (Integer key : collect1) {
            builder.add(map.get(key));
        }
        System.out.println(builder);
    }

    static void p1(){
        Scanner in = new Scanner(System.in);
        int max = Integer.parseInt(in.nextLine());
        int len = Integer.parseInt(in.nextLine());
        String task = in.nextLine();
        in.close();
        String[] newTask = task.split(" ");
        // 当前任务数, i 时间
        int cur = 0, i = 0;
        do {
            if (i < len) {
                cur += Integer.parseInt(newTask[i]);
            }
            if (cur <= max) {
                cur = 0;
            } else {
                cur -= max;
            }
            i++;
        } while (cur > 0 || i < len);
        System.out.println(i);
    }
}
